package com.sjtu.network.chatanalysis.controller

import com.sjtu.network.chatanalysis.model.Request
import com.sjtu.network.chatanalysis.model.Response
import com.sjtu.network.chatanalysis.openai.ChatGPTUtil
import com.sjtu.network.chatanalysis.openai.entity.ChatAnalysis
import com.sjtu.network.chatanalysis.openai.entity.chat.Message
import com.sjtu.network.chatanalysis.service.ScoreService
import com.sjtu.network.chatanalysis.util.ChatUtil.getScore
import com.sjtu.network.chatanalysis.util.ChatUtil.hashStringToInt
import com.sjtu.network.chatanalysis.util.ChatUtil.mapToRange
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.math.BigInteger
import java.security.MessageDigest


@RestController
class ChatAnalysisController(
    private val scoreService: ScoreService
) {

    private val logger = LoggerFactory.getLogger(ChatAnalysisController::class.java)

    @Autowired
    private lateinit var chatGPTUtil: ChatGPTUtil

    @RequestMapping("/score")
    fun chatScore(@RequestBody(required = false) request: Request): Response {
        println(request.content)
        val message: Message = chatGPTUtil.chat(request.content, "user")

        println(message.content)
        return Response(getScore(message.content))
    }

    @PostMapping("/chat/analysis")
    fun chatAnalysis(@RequestBody request: Request) : Response {
        logger.info("input: ${request.content}")

        return Response(scoreService.score(request.content))
    }

}